const Koa = require('koa')
const app = new Koa()
const router = require('./router/index')
const { koaBody } = require('koa-body')
const cors = require('@koa/cors')

// app.use(cors())
const httpCORS = async (ctx, next) => {
    ctx.set({
        "Access-Control-Allow-Origin": "*"
    })
    await next()
}

app.use(httpCORS)


// koa-body这个中间件一定要在路由中间件的前面！！
app.use(koaBody())
// 挂载路由中间件
app.use(router.routes())



// app.use(async ctx => {
//     console.log(ctx.path)
//     console.log(ctx.url)
//     if (ctx.method === 'POST' && ctx.path === '/login') {
//         ctx.body = '登录页'
//     } else if (ctx.method === 'GET' && ctx.path === '/user') {
//         ctx.body = '用户信息'
//     }
// })

const getIpv4 = require('./utils/index')
app.listen(3000, () => {
    console.log(`my server is running at http://${getIpv4()}:3000`)
})

